#include "manager.h"

//extern "C" CFG_EXPORT bool AddAuthority(QString sLevel,QString &auth);
//extern "C" CFG_EXPORT bool DelAuthority(QString sLevel,QString &auth);
//extern "C" CFG_EXPORT bool CheckSetting(QString ObjName,QString user);
//extern "C" CFG_EXPORT bool CheckUserPwd(QString user,QString pwd);
//extern "C" CFG_EXPORT bool CheckUser(QString user);
//extern "C" CFG_EXPORT bool AddUser(QString sLevel,QString user,QString pwd);
//extern "C" CFG_EXPORT bool DelUser(QString user);
//extern "C" CFG_EXPORT bool GetUser(QString sLevel,QString &user);

typedef bool (*tagAddAuthority)(QString sLevel,QString auth);
typedef bool (*tagDelAuthority)(QString sLevel,QString auth);

typedef bool (*tagCheckSetting)(QString ObjName,QString user);
typedef bool (*tagCheckUserPwd)(QString user,QString pwd);
typedef bool (*tagCheckUser)(QString user);
typedef bool (*tagAddUser)(QString sLevel,QString user,QString pwd);
typedef bool (*tagDelUser)(QString user);
typedef std::string (*tagGetUser)(QString sLevel);

tagAddAuthority _AddAuthority;
tagDelAuthority _DelAuthority;

tagCheckSetting _CheckSetting= nullptr;
tagCheckUserPwd _CheckUserPwd= nullptr;
tagCheckUser _CheckUser= nullptr;
tagAddUser _AddUser= nullptr;
tagDelUser _DelUser= nullptr;
tagGetUser _GetUser= nullptr;
Manager::Manager(QObject *parent) : QObject(parent)
{
    qDebug() << __FUNCTION__;
    _mylib.setFileName("Manager");
    _user = "";
    _libOK = false;
    if(_mylib.load()){
        qDebug() << "Manager.dll Load OK.";

        _AddAuthority = (tagCheckSetting)_mylib.resolve("AddAuthority");
        _DelAuthority = (tagCheckUserPwd)_mylib.resolve("DelAuthority");
        _CheckSetting = (tagCheckSetting)_mylib.resolve("CheckSetting");
        _CheckUserPwd = (tagCheckUserPwd)_mylib.resolve("CheckUserPwd");
        _CheckUser = (tagCheckUser)_mylib.resolve("CheckUser");
        _AddUser = (tagAddUser)_mylib.resolve("AddUser");
        _DelUser = (tagDelUser)_mylib.resolve("DelUser");
        _GetUser = (tagGetUser)_mylib.resolve("GetUser");

        if(_CheckSetting == nullptr||_CheckUserPwd == nullptr||_CheckUser == nullptr||
           _AddUser == nullptr||_DelUser == nullptr ||_GetUser == nullptr||
           _AddAuthority == nullptr|| _DelAuthority==nullptr)
            qDebug() << " Function Point is nullptr !!!!!";
        else
            _libOK = true;
    }
    else
        qDebug() << "Manager.dll Load Error";

}
Manager::~Manager()
{

}
void Manager::SetCheckUse(QString user)
{
    _user = user;
}

bool Manager::CheckSetting(QString ObjName,QString user)
{
    qDebug() << __FUNCTION__ << ObjName << user;

    bool ret = false;
    if(_libOK)
    ret = _CheckSetting(ObjName,user);

    qDebug() << ret;
    return ret;
}
bool Manager::CheckSetting(QString ObjName)
{
    qDebug() << __FUNCTION__ << ObjName;

    bool ret = false;
    if(_user!= "")
        if(_libOK)
        ret = _CheckSetting(ObjName,_user);
    else
        qDebug() << "_user is empty!!!";

    qDebug() << ret;
    return ret;
}
bool Manager::CheckUserPwd(QString user,QString pwd)
{
    qDebug() << __FUNCTION__;

    bool ret = false;
    if(_libOK)
    ret = _CheckUserPwd(user,pwd);
    qDebug() << ret;
    return ret;
}
bool Manager::CheckUser(QString user)
{
    qDebug() << __FUNCTION__;

    bool ret = false;
    if(_libOK)
    ret = _CheckUser(user);
    qDebug() << ret;
    return ret;
}
bool Manager::AddUser(ENUM_LEVEL Level,QString user,QString pwd)
{
    qDebug() << __FUNCTION__;

    bool ret = false;
    QString sLevel = QString("LEVEL%1").arg(Level);
    if(_libOK)
    ret = _AddUser(sLevel,user,pwd);
    qDebug() << ret;
    return ret;
}
bool Manager::DelUser(QString user)
{
    qDebug() << __FUNCTION__;

    bool ret = false;
    if(_libOK)
    ret = _DelUser(user);
    qDebug() << ret;
    return ret;
}

bool Manager::GetUser(ENUM_LEVEL Level,QString &user)
{
    qDebug() << __FUNCTION__;

    bool ret = false;
    QString sLevel = QString("LEVEL%1").arg(Level);
    std::string tmp;
    if(_libOK)
    tmp = _GetUser(sLevel);
    user=tmp.c_str();
    qDebug() << ret << user;
    return ret;
}
bool Manager::AddAuthority(ENUM_LEVEL Level,QString auth)
{
    qDebug() << __FUNCTION__;

    QString sLevel = QString("LEVEL%1").arg(Level);
    bool ret = false;
    if(_libOK)
    ret = _AddAuthority(sLevel,auth);
    qDebug() << ret;
    return ret;
}
bool Manager::DelAuthority(ENUM_LEVEL Level,QString auth)
{
    qDebug() << __FUNCTION__;

    QString sLevel = QString("LEVEL%1").arg(Level);
    bool ret = false;
    if(_libOK)
    ret = _DelAuthority(sLevel,auth);
    qDebug() << ret;
    return ret;
}
